<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\logic;

/**
 * 职位逻辑
 */
class TenPosition extends PlatBase
{
    
    // 面包屑
    public static $crumbs       = [];
    
    // 职位Select结构
    public static $deptSelect   = [];

    /**
     * 获取职位列表
     */
    public function getTenPositionList($where = [], $field = true, $order = 'sort asc', $paginate = DB_LIST_ROWS)
    {
        return $this->modelTenPosition->getList($where, $field, $order, $paginate);
    }

    /**
     * 获取职位信息
     */
    public function getTenPositionInfo($where = [], $field = true)
    {
        return $this->modelTenPosition->getInfo($where, $field);
    }
    
    /**
     * 职位添加
     */
    public function tenPositionAdd($data = [])
    {
        $validate_result = $this->validateTenPosition->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenPosition->getError()];
        }

        $result = $this->modelTenPosition->setInfo($data);

        $result && ten_log('新增', '新增职位，name：' . $data['name']);
        
        $url = url('show', ['pid' => $data['pid'] ? $data['pid'] : 0]);
        
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenPosition->getError()];
    }
    
    /**
     * 职位编辑
     */
    public function tenPositionEdit($data = [])
    {
        $validate_result = $this->validateTenPosition->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenPosition->getError()];
        }
        $url = url('show');
        $result = $this->modelTenPosition->setInfo($data);
        $result && ten_log('编辑', '编辑职位，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelTenPosition->getError()];
    }
    
    /**
     * 职位删除
     */
    public function tenPositionDel($where = [])
    {
        $result = $this->modelTenPosition->deleteInfo($where,true);
        $result && ten_log('删除', '删除职位，where：' . http_build_query($where));
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenPosition->getError()];
    }

    /**
     * 获得所有指定id所有父级
     * @param int $deptid
     * @param array $data
     * @return array
     */
    public function getTenPositionAllPid($deptid=0, $data=[])
    {
        $where['id']=['=',$deptid];
        $info = $this->modelTenPosition->getInfo($where,true);
        if(!empty($info) && $info['pid']){
            $data[]=$info['pid'];
            return $this->getTenPositionAllPid($info['pid'],$data);
        }
        return $data;
    }

    /**
     * 获得所有指定id所有子级
     * @param int $deptid
     * @param array $data
     * @return array
     */
    public function getTenPositionAllSon($deptid=0, $data=[])
    {
        $where['pid']=['=',$deptid];
        $sons = $this->modelTenPosition->getList($where,true,'sort asc',false);
        if (count($sons) > 0) {
            foreach ($sons as $v) {
                $data[] = $v['id'];
                $data = $this->getTenPositionAllSon($v['id'], $data); //注意写$data 返回给上级
            }
        }
        if (count($data) > 0) {
            return $data;
        } else {
            return false;
        }
        return $data;
    }

    //得到tree的数据
    public function getTenPositionListTree($where = [], $field = "id,name,pid", $order = 'sort asc', $paginate = false){

        $list=$this->getTenPositionList($where,$field,$order,$paginate)->toArray();
        $tree=list2tree($list);
        return $tree;
    }

    //得到tree的数据
    public function getTenPositionTreeSelect($where = [], $field = "id,name,pid", $order = 'sort asc', $paginate = false){
        $list=$this->getTenPositionList($where,$field,$order,$paginate)->toArray();
        $data=list2select($list);
        return $data;
    }
    /**
     * Checkbox,用于授权勾选
     *
     * @param $dataTreeList
     * @param $child
     * @param $rules_array
     * @return string
     * @author: 开发人生 goodkfrs@qq.com
     * @Time: 2025/3/9 11:36
     */
    public function tenPositionTreeCheckboxView($dataTreeList = [], $child = 'child', $rules_array = [])
    {
        $html_view = '';
        $rules_array = is_array($rules_array) ? $rules_array : explode(',', $rules_array);
        foreach ($dataTreeList as $oneInfo) {
            $icon = '';
            $checkbox_select = in_array($oneInfo['id'], $rules_array) ? "checked='checked'" : '';
            if (!empty($oneInfo[$child])) {
                $html_view .= '<li class="has_child">';
                $html_view .= "<span><i class='icon-minus-sign'></i></span>
                                <input class='rules_all' type='checkbox' name='position_ids[]' value='" . $oneInfo['id'] . "' $checkbox_select >
                                <i class='fa $icon'></i> " . $oneInfo['name'] . "";
                $html_view .= "<ul>";
                $html_view .= $this->tenPositionTreeCheckboxView($oneInfo[$child], $child, $rules_array);
                $html_view .= "</ul>";
                $html_view .= "</li>";
            } else {
                $html_view .= "<li class='dont-check-parent'>";
                $html_view .= "<span><i></i></span>
                                <input type='checkbox' name='position_ids[]' value='" . $oneInfo['id'] . "'  $checkbox_select >
                                <i class='fa $icon'></i> " . $oneInfo['name'] . "";
                $html_view .= "</li>";
            }
        }
        return $html_view;
    }

}